#include <tinx/string.h>
#include <tinx/stdio.h>

char buf[1024];

int main(int argc, char *argv[])
{
    dentry_t entry;
    stat_t statbuf;

    // - 1 是为了留一个 EOS
    char *ptr = (char *)buf + sizeof(buf) - 1;

    while (1)
    {
        fd_t fd = open("./", O_RDONLY, 0);

        fstat(fd, &statbuf);
        idx_t nr = statbuf.nr;
        dev_t dev = statbuf.dev;

        close(fd);

        chdir("../");
        fd = open("./", O_RDONLY, 0);
        fstat(fd, &statbuf);

        while (1)
        {
            int len = readdir(fd, &entry, 1);
            if (!entry.nr)
                continue;
            if (entry.nr == nr && statbuf.dev == dev)
                break;
            if (statbuf.dev != dev)
            {
                fd_t efd = open(entry.name, O_RDONLY, 0);
                stat_t estat;
                fstat(efd, &estat);
                close(efd);
                if (estat.dev == dev && estat.nr == nr)
                    break;
            }
        }

        if (!strcmp(entry.name, ".") || !strcmp(entry.name, ".."))
            break;

        ptr--;
        ptr[0] = '/';
        ptr -= entry.namelen;
        memcpy(ptr, entry.name, entry.namelen);
    }
    ptr--;
    ptr[0] = '/';

    printf("%s\n", ptr);

    return 0;
}